package com.kongyz.config;

import java.io.IOException;
import java.util.Random;
import java.util.concurrent.TimeUnit;

/**
 * @author kongyz
 * @date 2018/7/19 22:09
 * @Description:
 */
public class ConfigUpdater {
    public static final String PATH="/config";
    private ActiveKeyValueStore store;
    private Random random=new Random();

    /**
     * 初始化时链接zk
     * @param host
     * @throws IOException
     * @throws InterruptedException
     */
    public ConfigUpdater(String host) throws IOException, InterruptedException {
        store=new ActiveKeyValueStore();
        store.connect(host);
        Thread.sleep(100);
    }

    public void run() throws InterruptedException {
        while (true){
            String value=random.nextInt(100)+"";
            store.write(PATH,value);
            System.out.printf("Set %s to %s \n",PATH,value);
            TimeUnit.SECONDS.sleep(random.nextInt(10));

        }
    }

    public static void main(String[] args) {
        try {
            ConfigUpdater configUpdater=new ConfigUpdater("192.168.88.66");
            configUpdater.run();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
}
